******************************************************************
*Working code for the Carbon Pricing and Firm-Level CO2 abatement 
******************************************************************
clear all

*cd "/RFS_replication_package/Martinsson_et_al_datasets"

use merge_UC_Serrano_firm_characteristics.dta   /*We merged firm characteristics data sourced by UC (Upplysningscentralen, a Swedish credit agency) and Serrano (sourced by Bisnode).*/

rename rbsni rbsni1 /*Rbsni is an industry classification code, originally in the UC file, but then in the UC-Serrano merged file. This renaming will be important in row 255 when rbsni will be renamed to sni2007 to be able to match the data with another data.*/

/*Merge firm data with CO2 emissions.*/

merge 1:1 bidnr year using "emissions_basic.dta" /*Emissions_basic contains the fossil heating emissions*/

drop _merge 

merge 1:1 bidnr year using arbetstallen_SNI_codes.dta   /*This file contains the primary SNI codes, generated by 1- match_SNI_arbetstallen_firmdata.do.*/
drop if _merge==2
drop _merge

*A chief challenge of the workflow was dealing with the different statistical nomenclature. We could, however, take advantage of the installation-level data where the SNI codes are given in the whole time series. When a nomenclature replaces the previous one, the SNI codes in both systems are available in some years. Main steps:
* (1) Where SNI2007 (the most recent) codes are available, copy the codes back to the starting year of the time series;
* (2) There are around 1.5 million observations without SNI codes. But many rbsni codes are not missing (Rbsni codes are industry codes in the merged UC-Serrano file), so I complement the missing values with them. The UC finishes in 1996, the Serrano starts in 1997. Furthermore, the industry codes are in SNI2007 back to 1997. So I can just copy the values after 1997 where they are available. Before 1997, the UC is the datasource, which does not include the SNI codes in SNI2007. The keys between SNI69-SNI92-SNI2002-SNI2007 help to identify the industry affiliations for the UC data.*/ 


*Step (1):*
g year2=-year  /*In this exercise, I complement the missing SNI codes if it was missing for some years but there are non-missing observations in the series.*/

sort bidnr year2, stable

g id=_n	
	su id, meanonly
		
	quietly{
	local i
	local s=`r(max)'-1
	forvalues i=1(1)`s'{
	local j=`i'+1
replace sni2007=sni2007[`i'] in `j' if sni2007[`i']!="" & sni2007[`j']=="" & bidnr[`i']==bidnr[`j']
		
		 } 
}
drop id

*We copy forward as sometimes the last years are missing:
sort bidnr year, stable

g id=_n	
	su id, meanonly
		
	quietly{
	local i
	local s=`r(max)'-1
	forvalues i=1(1)`s'{
	local j=`i'+1
replace sni2007=sni2007[`i'] in `j' if sni2007[`i']!="" & sni2007[`j']=="" & bidnr[`i']==bidnr[`j']
		
		 } 
}

drop id
*Step 2*
tostring rbsni1, replace


g id=_n	
	su id, meanonly
		
	quietly{
	local i
	local s=`r(max)'
	forvalues i=1(1)`s'{

replace sni2007=rbsni1[`i'] in `i' if sni2007[`i']=="" & rbsni1[`i']!="." & year[`i']>=1997
		
		 } 
}

drop id

sort bidnr year2, stable



g id=_n	
	su id, meanonly
		
	quietly{
	local i
	local s=`r(max)'-1
	forvalues i=1(1)`s'{
	local j=`i'+1
replace sni2007=sni2007[`i'] in `j' if sni2007[`i']!="" & sni2007[`j']=="" & bidnr[`i']==bidnr[`j']
		
		 } 
}

rename rbsni1 sni92

*The correspondence table is necessary for those firms/installations that do not have sni2007 code. Correspondence_92_2007.dta: We counted the number of sni2007 codes that belong to sni92 codes, and chose the one with the maximum value. *
merge m:1 sni92 using correspondence_92_2007_20231014.dta /*Correspondence_92_2007 is generated in 2 - industry classification*/

drop if _merge==2

replace sni2007=sni2007_1 if sni2007=="" & sni2007_1!=""	/*We replaced the missing sni2007 codes based on the approximation with correspondence_92_2007.dta*/
drop _merge id sni2007_1

sort bidnr year, stable
rename sni92 sni69
rename sni2007 sni2007_1
*We do the same: use correspondence table to infer sni codes before 1994 (sni 69)*
*destring sni69, replace
merge m:1 sni69 using correspondence_69_2007_20231014.dta   /*Correspondence_69_2007 is generated in 2 - industry classification*/

replace sni2007_1=sni2007 if sni2007_1=="" & sni2007!=""	 /*We replaced the missing sni2007 codes based on the approximation with correspondence_69_2007.dta*/


drop _merge
sort bidnr year, stable

drop sni2007
rename sni2007_1 sni2007
rename sni69 rbsni1


*We copy sni2007 codes forward and backward to have as many industry classifications as possible.*
g id=_n	
	su id, meanonly
		
	quietly{
	local i
	local s=`r(max)'-1
	forvalues i=1(1)`s'{
	local j=`i'+1
replace sni2007=sni2007[`i'] in `j' if sni2007[`i']!="" & sni2007[`j']=="" & bidnr[`i']==bidnr[`j']
		
		 } 
}

drop id

sort bidnr year2, stable



g id=_n	
	su id, meanonly
		
	quietly{
	local i
	local s=`r(max)'-1
	forvalues i=1(1)`s'{
	local j=`i'+1
replace sni2007=sni2007[`i'] in `j' if sni2007[`i']!="" & sni2007[`j']=="" & bidnr[`i']==bidnr[`j']
		
		 } 
}
/*A final unification of the codes to ensure that sni2007 codes are uniform across time.*/
drop id
g id=_n	
	su id, meanonly
		
	quietly{
	local i
	local s=`r(max)'-1
	forvalues i=1(1)`s'{
	local j=`i'+1
replace sni2007=sni2007[`i'] in `j' if sni2007[`i']!=sni2007[`j'] & bidnr[`i']==bidnr[`j']
		
		 } 
}

*Agriculture and mining have 5-digit sni2007 codes, starting with "0", but those are dropped when codes are destringed*
g strleng=strlen(sni2007)
replace sni2007="0"+sni2007 if strleng==4
drop strleng 

*The primary analysis of interest is to create sni codes on 3 and 4-digit level*
g sni2007_4d=substr(sni2007,1,4)
	g sni2007_3d=substr(sni2007,1,3)
	g sni2007_2d=substr(sni2007_4d,1,2)
	destring sni2007_2d, replace
drop if bidnr==.

rename sni2007 rbsni

*We inferred energy and carbon taxes for 1991 and 92, but we had data on 2-digit level.*

merge m:1 rbsni using correspondence_2dsni69_sni2007_20231014.dta /*Code for this correspondence table can be found in 2- Industry classification*/

drop if _merge==2

drop _merge
rename sni69 sni69_5d
tostring sni69_5d, replace
g sni69=substr(sni69_5d,1,2)


destring sni69, replace
merge m:1 year sni69 using energy_tax_fuels.dta /*Hand-collected based on Energy Agency's data*/

drop _merge

*We calculate energy tax to apply the 1.2% rule and identify who will receive tax decrease*

foreach var of varlist kol koks lpg naturgas stadsgas bensin dieselolja eldn1 eldn2_3 eldn4_6     {
g energy_`var'=`var'*kt_co2foss 
		
		 } 

		 
g Energy_tax=energy_kol+energy_koks+energy_lpg+energy_naturgas+energy_stadsgas+energy_bensin+energy_dieselolja+energy_eldn1+energy_eldn2_3+energy_eldn4_6	


/*We merge with the carbon tax rates, then compute the carbon tax. Finally, we employ the exceptions to obtain the approximated carbon tax. The tax rates are nominal, so the deflation takes place in the end. */

merge m:1 year using carbon_tax_rates_new.dta  /*Hand-collected carbon tax rates*/
/*destring rbsni, replace
keep if rbsni>=5100 & rbsni<=33200*/

drop _merge

/*Adjustments in sales to the length of fiscal years*/

g ntoms_12month=ntoms*365/days

g carbon_tax=kt_co2foss*1000000*Tax_rate	
g carbon_tax_marg=kt_co2foss*1000000*Tax_rate /*We will use this variable as well to make sure we do not overwrite the original marginal taxes when we compute the exceptions*/
	
*g energy_intensive=1 if carbon_tax_marg/ntoms_12month>=0.005 & year>=1993
*replace energy_intensive=0 if carbon_tax_marg/ntoms_12month<0.005 & year>=1993

replace carbon_tax=0 if carbon_tax==.
replace carbon_tax_marg=0 if carbon_tax_marg==.
replace Energy_tax=0 if Energy_tax==.

/*Exceptions*/
g marginal_tax_rate=Tax_rate /*We start with the nominal tax rate, and then we change when needed*/
g ratio_carbon_energy=carbon_tax_marg/(carbon_tax_marg+Energy_tax) /*The sum of energy and carbon tax will be the basis of exceptions. We approximate the carbon tax based on the ratio of carbon tax to the sum. See below*/

*In 1991*
*If we observe energy tax, then we approximate carbon tax with the difference between the threshold and energy tax:
replace carbon_tax=0.017*ntoms_12month-Energy_tax if year==1991 &carbon_tax_marg!=0 & Energy_tax+carbon_tax_marg>0.017*ntoms_12month& 0.017*ntoms_12month-Energy_tax>=0 & ntoms_12month>0 

*If we do not observe energy tax, then we use the ratio to approximate carbon tax:
replace carbon_tax=ratio_carbon_energy*0.017*ntoms_12month if year==1991 &carbon_tax_marg!=0 & Energy_tax+carbon_tax_marg>0.017*ntoms_12month& 0.017*ntoms_12month-Energy_tax<0 & ntoms_12month>0 & ratio_carbon_energy!=.

*If firms do not qualify for exemptions*
replace carbon_tax=carbon_tax_marg if year==1991 & carbon_tax_marg!=0 & Energy_tax+carbon_tax_marg<=0.017*ntoms_12month

*Marginal tax if firms do not qualify for exemptions
replace marginal_tax_rate=Tax_rate if year==1991 & carbon_tax_marg!=0 & Energy_tax+carbon_tax_marg<=0.017*ntoms_12month & ntoms_12month>0

*Marginal tax if firms qualify for exemptions
replace marginal_tax_rate=0 if year==1991 & carbon_tax_marg!=0 & Energy_tax+carbon_tax_marg>0.017*ntoms_12month & ntoms_12month>0 

*In 1992*
*If we observe energy tax, then we approximate carbon tax with the difference between the threshold and energy tax:
replace carbon_tax=0.012*ntoms_12month-Energy_tax if year==1992 & carbon_tax_marg!=0 & Energy_tax+carbon_tax_marg>0.012*ntoms_12month & 0.012*ntoms_12month-Energy_tax>=0 & ntoms_12month>0 

*If we do not observe energy tax, then we use the ratio to approximate carbon tax:
replace carbon_tax=ratio_carbon_energy*0.012*ntoms_12month if year==1992 &carbon_tax_marg!=0 & Energy_tax+carbon_tax_marg>0.012*ntoms_12month& 0.012*ntoms_12month-Energy_tax<0 & ntoms_12month>0 & ratio_carbon_energy!=. 

*Marginal tax if firms do not qualify for exemptions
replace marginal_tax_rate=Tax_rate if year==1992 & carbon_tax_marg!=0 & Energy_tax+carbon_tax_marg<=0.012*ntoms_12month & ntoms_12month>0 & ntoms_12month!=.

*Marginal tax if firms qualify for exemptions
replace marginal_tax_rate=0 if year==1992 &  carbon_tax_marg!=0 & Energy_tax+carbon_tax_marg>0.012*ntoms_12month & ntoms_12month>0 & ntoms_12month!=.

/*Cement, kalk sector*/
g cement=0  /*We define firms in the cement sector based on their industry classification codes*/
replace cement=1 if rbsni=="23510" | rbsni=="23610" | rbsni=="23200" | rbsni=="23650" |rbsni=="23520" | rbsni=="23130" | rbsni=="23190" | rbsni=="23140" | rbsni==	"23120" | rbsni=="23910" | rbsni=="23701" | rbsni=="23709" 


*Between 1993 and 1996*

*If firms qualify for exemptions:
replace carbon_tax=0.012*ntoms_12month if year>=1993 & year<=1996 & carbon_tax_marg>0.012*ntoms_12month & cement==1 & ntoms_12month>0

*If firms do not qualify for exemptions (cement and non-cement firms respectively):
replace carbon_tax=carbon_tax_marg if year>=1993 & year<=1996 & carbon_tax_marg<=0.012*ntoms_12month & cement==1 & ntoms_12month>0 /*Nothing changes here, just to make sure all cases are considered*/
replace carbon_tax=carbon_tax_marg if year>=1993 & year<=1996 & cement==0 /*Nothing changes here, just to make sure all cases are considered*/

*Marginal tax if firms do not qualify for exemptions but cement firms*
replace marginal_tax_rate=Tax_rate if year>=1993 & year<=1996 & carbon_tax_marg<=0.012*ntoms_12month & cement==1 & ntoms_12month>0

*Marginal tax is firms qualify for exemptions and cement firms*
replace marginal_tax_rate=0 if year>=1993 & year<=1996 & carbon_tax_marg>0.012*ntoms_12month & cement==1 & ntoms_12month>0

*Marginal tax for non-cement firms*
replace marginal_tax_rate=Tax_rate if year>=1993 & year<=1996 & cement==0 & ntoms_12month>0


*Between 1997 and 2006*
*If firms qualify for exemptions*
replace carbon_tax=0.008*ntoms_12month+(carbon_tax_marg-0.008*ntoms_12month)*0.24*Tax_rate if year>=1997 & year<=2006 & carbon_tax_marg>0.008*ntoms_12month & ntoms_12month>0 

*If firms do not qualify for exemptions*
replace carbon_tax=carbon_tax_marg if year>=1997 & year<=2006 & carbon_tax_marg<=0.008*ntoms_12month & ntoms_12month>0 /*This should be true for all firms below the threshold regardless whether they are cement or energy intensive corporations*/

*Carbon tax for cement firms: they fall under both 0.8 and 1.2 percent rules. The former is applied first, and the latter then*

replace carbon_tax=0.008*ntoms_12month+(carbon_tax_marg-0.008*ntoms_12month)*0.24*Tax_rate if year>=1997 & year<=2006 & 0.008*ntoms_12month+(carbon_tax_marg-0.008*ntoms_12month)*0.24*Tax_rate<=0.012*ntoms_12month & 0.008*ntoms_12month+(carbon_tax_marg-0.008*ntoms_12month)*0.24*Tax_rate>0.008*ntoms_12month & ntoms_12month>0 & cement==1
replace carbon_tax=0.012*ntoms_12month if year>=1997 & year<=2006  & 0.008*ntoms_12month+(carbon_tax_marg-0.008*ntoms_12month)*0.24*Tax_rate>0.012*ntoms_12month & ntoms_12month>0 & cement==1



*Marginal tax if firms do not qualify for exemptions*
replace marginal_tax_rate=Tax_rate if year>=1997 & year<=2006 & carbon_tax_marg<=0.008*ntoms_12month & ntoms_12month>0 

*Marginal tax if firms qualify for exemptions*
replace marginal_tax_rate=Tax_rate*0.24 if year>=1997 & year<=2006 & carbon_tax_marg>0.008*ntoms_12month & ntoms_12month>0 

*Marginal tax for cement firms, 1997-2006:*
*1) Marginal tax if under the threshold for 0.8 percent rule*
replace marginal_tax_rate=Tax_rate if year>=1997 & year<=2006 & cement==1 & carbon_tax_marg<=0.008*ntoms_12month & ntoms_12month>0

*2) Marginal tax if the firm is between the threshold for 0.8 percent rule and the 1.2 percent rule:
replace marginal_tax_rate=Tax_rate*0.24 if year>=1997 & year<=2006 & cement==1 & 0.008*ntoms_12month+(carbon_tax_marg-0.008*ntoms_12month)*0.24*Tax_rate<=0.012*ntoms_12month & 0.008*ntoms_12month+(carbon_tax_marg-0.008*ntoms_12month)*0.24*Tax_rate>0.008*ntoms_12month & ntoms_12month>0& ntoms_12month>0

*3) Marginal tax above the threshold for the 1.2 percent rule
replace marginal_tax_rate=0 if year>=1997 & year<=2006 & cement==1 & 0.008*ntoms_12month+(carbon_tax_marg-0.008*ntoms_12month)*0.24	>0.012*ntoms_12month & ntoms_12month>0

					
/*Now merge with the PPI data.*/

*Merging with the producer price index data*
merge m:1 sni2007_4d year using SNI_SPIN_PPI.dta  /*Producer price index, retrieved from Statistics Sweden, "Producer Price Index by market and products, SPIN 2015"*/

keep if _merge==3
drop _merge

/*g sni2007_2d=substr(sni2007_4d,1,2)
destring sni2007_2d, replace*/
sort sni2007_2d year, stable
	
	
	
/*Merging with EU ETS data*/										

*Average EU emission right prices*
g euets_price=0.2486 if year==2008
replace euets_price=0.1462 if year==2009
replace euets_price=0.1374 if year==2010
replace euets_price=0.1137 if year==2011
replace euets_price=0.0641 if year==2012
replace euets_price=0.0386 if year==2013
replace euets_price=0.0546 if year==2014
replace euets_price=0.0719 if year==2015
replace euets_price=0.0506 if year==2016
replace euets_price=0.0563 if year==2017


merge 1:1 year bidnr using installation_firm_emissions_2.dta	/*Merge with emissions on workplace level, see code for "installation_firm_emissions_2.dta" in "5- installation_firm_emissions_datawork"*/ 
drop if _merge==2
replace ets_emission=ets_emission*1000 /*ETS emissions are given in metric tonnes, but we need those in kg*/
g eu=0 /*Indicator variable with value=1 if the installation is covered by the EU ETS*/
/*replace buts=0 if buts==.*/
replace eu=1 if _merge==3

drop _merge

drop id

/*We copy the indicator to make sure firms under the EU ETS will be marked*/
sort bidnr year, stable
g id=_n	
	su id, meanonly
		
	quietly{
	local i
	local s=`r(max)'-1
	forvalues i=1(1)`s'{
	local j=`i'+1
replace eu=1 in `j' if eu[`i']==1 & eu[`j']==0 & bidnr[`i']==bidnr[`j']
		
		 } 
}



/*Between 2007 and 2010, not EU ETS installations*/

*If firms quality for exemptions:
replace carbon_tax=0.008*ntoms_12month+(carbon_tax_marg-0.008*ntoms_12month)*0.24*Tax_rate if year>=2007 & year<=2010 & eu==0 & carbon_tax_marg>0.008*ntoms_12month & ntoms_12month>0
*If firms do not qualify for exemptions:
replace carbon_tax=carbon_tax_marg if year>=2007 & year<=2010 & carbon_tax_marg<=0.008*ntoms_12month & ntoms_12month>0 & eu==0 			
			

*Marginal tax rate if firms do not qualify for exemptions:
replace marginal_tax_rate=Tax_rate if year>=2007 & year<=2010 & carbon_tax_marg<=0.008*ntoms_12month & ntoms_12month>0 & eu==0 
*Marginal tax rate if firms qualify for exemptions:
replace marginal_tax_rate=Tax_rate*0.24 if year>=2007 & year<=2010 & carbon_tax_marg>0.008*ntoms_12month & ntoms_12month>0 & eu==0 


/*Between 2011 and 2014, not EU installation*/

*If firms quality for exemptions:
replace carbon_tax=0.012*ntoms_12month+(carbon_tax_marg-0.012*ntoms_12month)*0.24*Tax_rate if year>=2011 & year<=2014 & carbon_tax_marg>0.012*ntoms_12month  & eu==0 & ntoms_12month>0 
*If firms do not qualify for exemptions:
replace carbon_tax=carbon_tax_marg if year>=2011 & carbon_tax_marg<=0.012*ntoms_12month & eu==0 

*Marginal tax rate if firms do not qualify for exemptions:
replace marginal_tax_rate=Tax_rate if year>=2011 & year<=2014 & carbon_tax_marg<=0.012*ntoms_12month & eu==0 & ntoms_12month>0
*Marginal tax rate if firms qualify for exemptions:
replace marginal_tax_rate=Tax_rate*0.24 if year>=2011 & year<=2014 & carbon_tax_marg>0.012*ntoms_12month  & eu==0 & ntoms_12month>0 

/*From 2015, not EU installation*/
*There are no exemptions after 2015
replace carbon_tax=carbon_tax_marg if year>=2015 & eu==0
replace marginal_tax_rate=Tax_rate if year>=2015 & eu==0



/*Carbon pricing between 2012 and 2014 for EU installations: Heating emissions are reported in emissions_basic, and the EU ETS file contains all emissions under the EU ETS (process and heating emissions). However, the emission values in both files may differ as EU ETS incorporates more types of emissions. In the EU ETS file, it is not reported separately how much emission is originating from heating and process emissions. 

If heating emissions are smaller than ETS emissions, then carbon tax paid is regarded as 0 as we assume that all emissions are covered by EU ETS. If EU ETS emissions are larger than heating emissions, then we take into account the difference of these. This approximation likely underestimates the real emission differences as EU ETS incorporates process emissions*/



*For 2007, EU ETS installations*/

*If firms qualify for exemptions*
replace carbon_tax=0.008*ntoms_12month+(carbon_tax_marg-0.008*ntoms_12month)*0.24*Tax_rate if year==2007 & eu==1 & carbon_tax_marg>0.008*ntoms_12month & ntoms_12month>0 
replace carbon_tax=carbon_tax_marg if year==2007 & carbon_tax_marg<=0.008*ntoms_12month & ntoms_12month>0 & eu==1			
			

*Marginal tax if firms do not qualify for exemptions*
replace marginal_tax_rate=Tax_rate if year==2007 & carbon_tax_marg<=0.008*ntoms_12month & ntoms_12month>0 & eu==1 

*Marginal tax if firms qualify for exemptions*
replace marginal_tax_rate=Tax_rate*0.24 if year==2007 & carbon_tax_marg>0.008*ntoms_12month & ntoms_12month>0  & eu==1 



/*Between 2008 and 2010, EU ETS installations*/

*If firms qualify for exemptions*
replace carbon_tax=0.008*ntoms_12month+(kt_co2foss*1000000*1.01*0.15-0.008*ntoms_12month)*0.24*1.01*0.15 if year==2008 & kt_co2foss*1000000*1.01*0.15>0.008*ntoms_12month & ntoms_12month>0 & eu==1 
replace carbon_tax=0.008*ntoms_12month+(kt_co2foss*1000000*1.05*0.15-0.008*ntoms_12month)*0.24*1.05*0.15 if year==2009 & kt_co2foss*1000000*1.05*0.15>0.008*ntoms_12month & ntoms_12month>0 & eu==1 
replace carbon_tax=0.008*ntoms_12month+(kt_co2foss*1000000*1.05*0.15-0.008*ntoms_12month)*0.24*1.05*0.15 if year==2010 & kt_co2foss*1000000*1.05*0.15>0.008*ntoms_12month & ntoms_12month>0 & eu==1 

*If firms do not qualify for exemptions*
replace carbon_tax=kt_co2foss*1000000*1.01*0.15 if year==2008 & kt_co2foss*1000000*1.01*0.15<=0.008*ntoms_12month & ntoms_12month>0 & eu==1 
replace carbon_tax=kt_co2foss*1000000*1.05*0.15 if year==2009 & kt_co2foss*1000000*1.05*0.15<=0.008*ntoms_12month & ntoms_12month>0 & eu==1 
replace carbon_tax=kt_co2foss*1000000*1.05*0.15 if year==2010 & kt_co2foss*1000000*1.05*0.15<=0.008*ntoms_12month & ntoms_12month>0 & eu==1 

*Marginal tax rate if firms qualify for exemptions*
replace marginal_tax_rate=1.01*0.15*0.24 if eu==1 & year==2008 & kt_co2foss*1000000*1.01*0.15>0.008*ntoms_12month
replace marginal_tax_rate=1.05*0.15*0.24 if eu==1 & year==2009 & kt_co2foss*1000000*1.05*0.15>0.008*ntoms_12month
replace marginal_tax_rate=1.05*0.15*0.24 if eu==1 & year==2010 & kt_co2foss*1000000*1.05*0.15>0.008*ntoms_12month

*Marginal tax rate if firms do not qualify for exemptions*
replace marginal_tax_rate=1.01*0.15 if eu==1 & year==2008 & kt_co2foss*1000000*1.01*0.15<=0.008*ntoms_12month
replace marginal_tax_rate=1.05*0.15 if eu==1 & year==2009 & kt_co2foss*1000000*1.05*0.15<=0.008*ntoms_12month
replace marginal_tax_rate=1.05*0.15 if eu==1 & year==2010 & kt_co2foss*1000000*1.05*0.15<=0.008*ntoms_12month


 /*Assuming that the eu ets file is comprehensive, if not_eu is computed and equals zero, all the Swedish emissions are assumed to be covered by the eu ets.*/
/*If there is no not_eu, then I assume it is not covered by the EU ETS*/


/*Carbon tax between 2011 and 2014, for EU installations*/

/*ETS emissions are from the installation-level emissions file. It is equal to total ets emission-process emissions-non CO2 emissions, so it is the CO2 under the EU ETS*/

*Carbon tax if the firm likely paid carbon tax and its level reached a threshold for tax rebates*
replace carbon_tax=0.012*ntoms_12month+((kt_co2foss*1000000-ets_emission*1000)*Tax_rate-0.012*ntoms_12month)*0.24*Tax_rate if eu==1 & year>=2011 & year<=2014 &(kt_co2foss*1000000-ets_emission*1000)>0 & (kt_co2foss*1000000-ets_emission*1000)*Tax_rate>0.012*ntoms_12month  & ntoms_12month>0

*Carbon tax if the firm likely paid carbon tax but did not get any exemptions
replace carbon_tax=(kt_co2foss*1000000-ets_emission*1000)*Tax_rate if eu==1 & year>=2011 & year<=2014 &(kt_co2foss*1000000-ets_emission*1000)>0 & (kt_co2foss*1000000-ets_emission*1000)*Tax_rate<=0.012*ntoms_12month  & ntoms_12month>0

*If the firms does not pay carbon tax:
replace carbon_tax=0 if eu==1 & year>=2011 & year<=2014 & ets_emission*1000-kt_co2foss*1000000>=0 & ntoms_12month>0


*Marginal tax if ets emissions are greater than heating emissions from the emission registry (i.e. no carbon tax is paid)
replace marginal_tax_rate=0 if eu==1 & year>=2011 & year<=2014 & ets_emission*1000-kt_co2foss*1000000>=0 

*Marginal tax if the firm has business activity under the carbon tax, but its level is not high enough to entitle the firm to receive tax rebates*
replace marginal_tax_rate=Tax_rate if eu==1 & year>=2011 & year<=2014 & (kt_co2foss*1000000-ets_emission*1000)>0 & (kt_co2foss*1000000-ets_emission*1000)*Tax_rate<=0.012*ntoms_12month & ntoms_12month>0

*Marginal tax if the firm has business activity under the carbon tax, and its level reaches entitles the firm to receive tax rebates*
replace marginal_tax_rate=Tax_rate*0.24 if eu==1 & year>=2011 & year<=2014 & (kt_co2foss*1000000-ets_emission*1000)>0 & (kt_co2foss*1000000-ets_emission*1000)*Tax_rate>0.012*ntoms_12month & ntoms_12month>0

*From 2015, EU installations*

*Carbon tax if there is no paid carbon tax
replace carbon_tax=0 if eu==1 & year>=2015 & ets_emission*1000-kt_co2foss*1000000>=0 & ntoms_12month>0

*Carbon tax if there is paid carbon tax
replace carbon_tax=(kt_co2foss*1000000-ets_emission*1000)*Tax_rate if eu==1 & year>=2015 & (kt_co2foss*1000000-ets_emission*1000)>0 & ntoms_12month>0

*Carbon tax if there is no paid carbon tax
replace marginal_tax_rate=0 if eu==1 & year>=2015 & ets_emission*1000-kt_co2foss*1000000>=0 

*Carbon tax if there is paid carbon tax
replace marginal_tax_rate=Tax_rate if eu==1 & year>=2015 & (kt_co2foss*1000000-ets_emission*1000)>0

*Overall costs*

*Total marginal costs that reflect EU ETS price and the marginal tax from carbon tax

*For EU ETS installations
g marginal_cost=marginal_tax_rate+euets_price if year>=2008 & eu==1

*For non-EU ETS installations
replace marginal_cost=marginal_tax_rate if year>=2008 & eu==0
replace marginal_cost=marginal_tax_rate if year<2008


*Average cost of carbon emission*

g average_cost=carbon_tax/(kt_co2foss*1000000) if year<2008
replace average_cost=carbon_tax/(kt_co2foss*1000000) if year>=2008 & eu==0
replace average_cost=((ets_emission*1000-sum_allowance*1000000)*euets_price + carbon_tax)/(kt_co2foss*1000000) if year>=2008 & eu==1 & ets_emission*1000-sum_allowance*1000000>0
replace average_cost=carbon_tax/(kt_co2foss*1000000) if year>=2008 & eu==1 & ets_emission*1000-sum_allowance*1000000<=0

/*Now merge with the PPI data.*/

merge m:1 sni2007_4d year using SNI_SPIN_PPI.dta

keep if _merge==3
drop _merge	

	destring sni2007_2d, replace
	sort sni2007_2d year, stable

	


g ppi_2010=PPI if year==2010 /*Base year is 2010.*/
egen ppi_2010_1=max(ppi_2010), by(sni2007_4d)
replace ppi_2010=PPI/ppi_2010_1 		




/*CPI: we adjust the energy and carbon tax values with inflation. CPI, below, is provided by Statistics Sweden, and the base year is 1980. Firm financials are adjusted with PPI.*/
g cpi=0
replace cpi =		153.80	if year	==	1985		
	replace	cpi =		160.30	if year	==	1986
	replace cpi =		167.00	if year ==	1987
	replace cpi =		176.70	if year ==	1988
	replace cpi =		188.10 	if year ==	1989
	replace cpi = 		207.80	if year ==	1990

	replace cpi = 		227.20	if year ==	1991

	replace cpi = 		232.40	if year ==	1992

	replace cpi = 		243.20	if year ==	1993

	replace cpi = 		248.50	if year ==	1994

	replace cpi = 		254.80	if year ==	1995

	replace cpi = 		256.00	if year ==	1996

	replace cpi = 		257.30	if year ==	1997
	replace cpi = 		257.0	if year ==	1998
	replace cpi = 		258.10	if year ==	1999
	replace cpi = 		260.70	if year ==	2000
	replace cpi = 		267.10	if year ==	2001
	replace cpi = 		272.80	if year ==	2002
	replace cpi = 		278.10	if year ==	2003
	replace cpi = 		279.20	if year ==	2004
	replace cpi = 		280.40	if year ==	2005
	replace cpi = 		284.22	if year ==	2006
	replace cpi = 		290.51	if year ==	2007
	replace cpi = 		300.61	if year ==	2008
	replace cpi = 		299.66	if year ==	2009
	replace cpi = 		303.46	if year ==	2010
	replace cpi = 		311.43	if year ==	2011
	replace cpi = 		314.2	if year ==	2012
	replace cpi = 		314.06	if year ==	2013
	replace cpi = 		313.49	if year ==	2014
	replace cpi = 		313.35	if year ==	2015
	replace cpi = 		316.43	if year ==	2016
	replace cpi = 		322.11	if year ==	2017

	g defl_2010=cpi/303.46
	
	g carbon_tax_defl=carbon_tax/defl_2010
	g Energy_tax_defl=Energy_tax/defl_2010
	/*replace ebit=ebit/ppi_2010
	g ebit_tax=ebit+carbon_tax
	replace cash_flow=cash_flow/ppi_2010
	replace resar=resar/ppi_2010*/
	g sales_ppi=ntoms/ppi_2010
    g sales_defl=ntoms/defl_2010
*We analyzed the effect of normalization with PPI and CPI.*
	
	g ebit_ppi=ebit/ppi_2010
	g ebit_defl=ebit/defl_2010
	
	g ebit_tax=ebit+carbon_tax
	g ebit_tax_defl=ebit_tax/defl_2010
	
	g cash_flow_ppi=cash_flow/ppi_2010
	g cash_flow_defl=cash_flow/defl_2010

	
		/*Co2 tax/quantities time series on firm-level*/
	
	/*g tax_sales=carbon_tax/ntoms
	g tax_ebit=carbon_tax/ebit	
	g tax_ebit2=carbon_tax/ebit_tax
	g tax_fcf=carbon_tax/cash_flow
	g tax_resar=carbon_tax/resar*/
	
	/*The same on industry level*/
	
	
	destring sni2007_2d, replace
	sort sni2007_2d year, stable
	*We created a few industry-level variables for analysis*
	egen tax_industry=sum(carbon_tax_defl), by(sni2007_2d year)
	egen sales_industry_ppi=sum(sales_ppi),by(sni2007_2d year)
	egen sales_industry_defl=sum(sales_defl), by(sni2007_2d year)
	
	g tax_sales_industry=tax_industry/sales_industry_defl
	
	egen ebit_industry_ppi=sum(ebit_ppi),by(sni2007_2d year)  /*EBIT*/
	egen ebit_industry_defl=sum(ebit_defl),by(sni2007_2d year)
	
	
	g tax_ebit_industry=tax_industry/ebit_industry_defl
	

	egen fcf_industry_ppi=sum(cash_flow_ppi),by(sni2007_2d year) /*Free cash flow*/
	egen fcf_industry_defl=sum(cash_flow_defl),by(sni2007_2d year)

	g tax_fcf_industry=tax_industry/fcf_industry_defl
	
	egen emission=sum(kt_co2foss), by(sni2007_2d year)
	g emission_intensity=emission*1000000/sales_industry_ppi
	
	/*The same variables with 12 month fiscal years*/
	g sales_ppi_12month=ntoms_12month/ppi_2010
    g sales_defl_12month=ntoms_12month/defl_2010
	
	egen sales_industry_ppi_12month=sum(sales_ppi_12month),by(sni2007_2d year)
	egen sales_industry_defl_12month=sum(sales_defl_12month), by(sni2007_2d year)
	
	g tax_sales_industry_12month=tax_industry/sales_industry_defl_12month	
	egen emission_4d=sum(kt_co2foss), by(sni2007_4d year)
	egen sales_industry_ppi_12month_4d=sum(sales_ppi_12month),by(sni2007_4d year)
	g ei_4d=emission_4d*1000000/sales_industry_ppi_12month_4d  /*Emission intensity, EI, for sni2007 on 4-digit*/
	/*replace sales_industry=sales_industry/1000000  /*For the tables in row 685-691*/
	replace carbon_tax=carbon_tax/1000000*/
	sort bidnr year, stable

	save masterfile.dta, replace

*First, save the master file: masterfile.dta. We want to match the file below with that.	
*Generating energy use, normalized emissions variables*

use "utslappsdata_scb_cfarnr.dta" , clear /*Establishment-level emission data, sourced by Statistic Sweden*/

table ar, stat(sum tj) stat(count tj)

*Normalized emissions: we can define this for each row as emissions are reported for each fuel*
g temp_emission_tj=kt_CO2foss/ tj

*Define high-carbon fuels:
g high=1 if temp_emission_tj>.09
replace high=0 if high==.
g high_energy=high*tj
g high_emi=high*kt_CO2foss

*Define mid-carbon fuels:
g mid=1 if temp_emission_tj<=0.09 & temp_emission_tj!=0
replace mid=0 if mid==.
g mid_energy=mid*tj
g mid_emi=mid*kt_CO2foss

drop temp_emission_tj

*Here are the non fossile fuels (5-17)

g low=1 if kt_CO2foss==0
replace low=0 if low==.
g low_energy=low*tj
g low_emi_bio=low*kt_CO2bio

*We will need to calculate high energy (energy that is from high CO2 sources at the fuel type level)
collapse (sum) tj kt_CO2foss high_energy high_emi mid_energy mid_emi low_energy low_emi_bio, by(PeOrgNr_LopNr ar)

duplicates drop PeOrgNr_LopNr ar, force  /*unique on orgnr ar level*/


*Then we can collapse the energy use, energy use from high fossile fuel and co2 emissions  

g emissions_tj= kt_CO2foss/ tj



*Match with the master file*
rename ar year
rename PeOrgNr_LopNr bidnr

merge m:1 bidnr year using "masterfile.dta"   

drop if _merge==1 /*we keep only the master file*/
drop _merge
sort bidnr year, stable 
*Save the merged file as :
save "master_file_with_fuels_noemissions.dta", replace

keep if kt_co2foss!=0
keep if kt_co2foss!=.

save "master_file_with_fuels_emissions.dta", replace




	
	
	
	 
	
	

	
	
	
	
	









